<template>
  <el-dialog
    :before-close="close"
    :width="dialogWidth"
    :title="title"
    :visible.sync="editDialog"
    v-if="editDialog"
    center
    v-dialogDrag
  >
    <el-form :model="form" ref="form" :rules="rules" class="mt20">
      <el-col :span="8" class="mb20">
        <el-form-item label="渠道：" prop="promotion" :label-width="formLabelWidth">
          <el-input v-model="form.promotion" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="时间：" prop="time" :label-width="formLabelWidth">
          <el-input v-model="form.time" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="客户电话：" prop="phone" label-width="90px">
          <el-input v-model="form.phone" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="中心：" prop="center" :label-width="formLabelWidth">
          <el-input v-model="form.center" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="部门：" prop="dept" :label-width="formLabelWidth">
          <el-input v-model="form.dept" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="业务员：" prop="salesman" label-width="90px">
          <el-input v-model="form.salesman" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="姓名：" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="区域：" prop="distribution" :label-width="formLabelWidth">
          <el-input v-model="form.distribution" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="定金(元)：" prop="deposit" label-width="78px">
          <el-input type="number" v-model="form.deposit" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="购买车型：" prop="cartype" label-width="95px">
          <el-input v-model="form.cartype" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="审过渠道：" prop="trial" label-width="82px">
          <el-input v-model="form.trial" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="购车类型：" prop="mortgage" label-width="82px">
          <el-input v-model="form.mortgage" autocomplete="off" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="实际收费(元)：" prop="charge" label-width="116px">
          <el-input type="number" v-model="form.charge" autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="成本(元)：" prop="cost" label-width="88px">
          <el-input type="number" v-model="form.cost" autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" class="mb20">
        <el-form-item label="业绩(元)：" prop="achievement" label-width="88px">
          <el-input
            type="number"
            v-model="form.achievement"
            autocomplete="off"
            @keyup.enter.native="handleConfirm('form')"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer align-center tiling inline-block mt20 mb20">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import "../utils/drag.js";

export default {
  name: "finance_dialog",
  data() {
    var validateCharge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入实际收费！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length < 1) {
        return callback(new Error("长度在 1-8 个数字"));
      } else if (value.length > 8) {
        return callback(new Error("长度在 1-8 个数字"));
      } else {
        callback();
      }
    };
    var validateCost = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入成本！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length < 1) {
        return callback(new Error("长度在 1-8 个数字"));
      } else if (value.length > 8) {
        return callback(new Error("长度在 1-8 个数字"));
      } else {
        callback();
      }
    };
    var validateAchievement = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入业绩！"));
      } else if (value.toString().search(/^[0-9]*$/) === -1) {
        return callback(new Error("请输入数字！"));
      } else if (value.length < 1) {
        return callback(new Error("长度在 1-8 个数字"));
      } else if (value.length > 8) {
        return callback(new Error("长度在 1-8 个数字"));
      } else {
        callback();
      }
    };
    return {
      form: {
        time: "",
        promotion: "",
        deposit: "",
        phone: "",
        name: "",
        center: "",
        dept: "",
        salesman: "",
        notsigning: "",
        distribution: "",
        cartype: "",
        mortgage: "",
        trial: "",
        charge: "",
        cost: "",
        achievement: ""
      },
      rules: {
        charge: { validator: validateCharge, trigger: "blur" },
        cost: { validator: validateCost, trigger: "blur" },
        achievement: { validator: validateAchievement, trigger: "blur" }
      },
      formLabelWidth: "70px",
      dialogWidth: "1000px"
    };
  },
  watch: {
    editDialog() {
      this.form = {
        time: this.financeForm.time,
        promotion: this.financeForm.promotionName,
        deposit: this.financeForm.deposit,
        phone: this.financeForm.phone,
        name: this.financeForm.name,
        center: this.financeForm.centerName,
        dept: this.financeForm.deptName,
        salesman: this.financeForm.salesmanName,
        notsigning: this.financeForm.notsign,
        distribution: this.financeForm.distributionName,
        cartype: this.financeForm.cartype,
        loanAmount: this.financeForm.loanAmount,
        warrantManId: this.financeForm.warrantManId,
        trial: this.financeForm.trial,
        charge: this.financeForm.charge ? this.financeForm.charge : "",
        cost: this.financeForm.cost ? this.financeForm.cost : "",
        achievement: this.financeForm.achievement
          ? this.financeForm.achievement
          : "",
        mortgage: this.financeForm.isMortgage
      };
    }
  },
  methods: {
    handleConfirm(formName) {
      // 先进行表单验证
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editFinance();
        }
      });
    },
    editFinance() {
      this.$axios
        .put("/api/finance/editFinance/" + this.financeForm.id, this.form)
        .then(data => {
          if (data.status) {
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.$emit("update");
            this.close();
          } else {
            this.$message({
              type: "error",
              message: data.msg
            });
          }
        });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        charge: "",
        cost: "",
        achievement: ""
      };
    }
  },
  props: ["editDialog", "title", "financeForm"]
};
</script>
<style scoped>
</style>
